package com.dy.nygl.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.dy.nygl.pojo.Equipment;
import com.dy.nygl.service.EquipmentService;

@Controller
@RequestMapping("/equipment")
public class EquipmentController {

	private int line = 15;
	private int startLine = 0;
	private int endLine;
	private int size = 0;
	private String sect1;
	private String search;
	private int page = 1;
	private int currentPage = 1;

	private List<Equipment> equipmentList = null;
	private List<Equipment> searchList = null;

	@Resource(name = "equipmentService")
	private EquipmentService equipmentService;

	@RequestMapping("/getList.action")
	public ModelAndView getList(HttpSession session) throws IOException {
		ModelAndView model = new ModelAndView();

		startLine = getStartLine();
		model.addObject("begin", startLine);
		line = getLine();
		endLine = startLine + line;
		model.addObject("end", endLine);

		equipmentList = getEquipmentList();
		searchList = getSearchList();

		size = getSize();
		page = getPage();
		currentPage = getCurrentPage();

		model.addObject("size", size);
		model.addObject("page", page);
		model.addObject("currentPage", currentPage);
		model.addObject("equipList", equipmentList);
		model.addObject("searchList", searchList);
		model.setViewName("equip_manage");
		return model;
	}

	@RequestMapping("/searchEquip.action")
	public void searchEquip(HttpServletResponse response, HttpSession session) throws ServletException, IOException {

		sect1 = getSect1();
		search = getSearch();
		searchList = null;
		searchList = equipmentService.selectEquip(sect1, search);
		setSearchList(searchList);

		PrintWriter out = response.getWriter();
		out.print(6);
	}

	@RequestMapping("/getSect1.action")
	public void getSect1(@RequestParam("sect1") String sect1, HttpServletResponse response, HttpSession session)
			throws ServletException, IOException {
		setSect1(sect1);
		PrintWriter out = response.getWriter();
		out.print(5);
	}

	@RequestMapping("/getSearch.action")
	public void getSearch(@RequestParam("search") String search, HttpServletResponse response, HttpSession session)
			throws ServletException, IOException {
		setSearch(search);
		PrintWriter out = response.getWriter();
		out.print(6);
	}

	@RequestMapping("/setList.action")
	public void setList(HttpServletResponse response, HttpSession session) throws IOException {
		equipmentList = equipmentService.selectAll();
		setEquipmentList(equipmentList);
		setSize(equipmentList.size());
		page = getSize() / line + 1;// ��ҳ��
		setPage(page);
		PrintWriter out = response.getWriter();
		out.print(6);
	}

	@RequestMapping("/nextPage.action")
	public void nextPage(@RequestParam("end") Integer end, HttpServletResponse response, HttpSession session)
			throws IOException {
		setStartLine(end + 1);
		currentPage = currentPage + 1;
		setCurrentPage(currentPage);
		PrintWriter out = response.getWriter();
		out.print(1);
	}

	@RequestMapping("/setIndex.action")
	public void setIndex(HttpServletResponse response, HttpSession session) throws ServletException, IOException {
		setStartLine(0);
		currentPage = 1;
		PrintWriter out = response.getWriter();
		out.print(2);
	}

	@RequestMapping("/lastPage.action")
	public void lastPage(@RequestParam("begin") Integer begin, HttpServletResponse response, HttpSession session)
			throws ServletException, IOException {
		line = getLine();
		if (begin - line < 0) {
			startLine = begin;
			setStartLine(startLine);
		} else {
			startLine = begin - line;
			currentPage = currentPage - 1;
			setCurrentPage(currentPage);
			setStartLine(startLine);
		}
		PrintWriter out = response.getWriter();
		out.print(3);
	}

	@RequestMapping("/setSwift.action")
	public void setSwift(@RequestParam("swift") String swift, HttpServletResponse response, HttpSession session)
			throws ServletException, IOException {
		String[] index = swift.split("=");
		String id1 = index[0];
		String swiftIs = index[1];
		String status = "";
		String switchT = "";
		if (swiftIs.equals("1")) {
			status = "已开启";
			switchT = "<div class='checkbox checkbox-slider--b'><label><input type='checkbox' name='payMethod' onclick='getradioo("
					+ id1 + "00)' checked><span></span></label></div>";
		} else if (swiftIs.equals("0")) {
			status = "已关闭";
			switchT = "<div class='checkbox checkbox-slider--b'><label><input type='checkbox' name='payMethod' onclick='getradioo("
					+ id1 + "01)'><span></span></label></div>";
		}

		int id = Integer.parseInt(id1);
		Equipment equipment = new Equipment();
		equipment.setId(id);
		equipment.setSwift(switchT);
		equipment.setStatus(status);
		equipmentService.update(equipment);

		PrintWriter out = response.getWriter();
		out.print(3);
	}

	@RequestMapping("/setSwift1.action")
	public void setSwift1(@RequestParam("swift") String swift, HttpServletResponse response, HttpSession session)
			throws ServletException, IOException {
		int len = swift.length();
		int indexOf = len - 1;
		String id1 = swift.substring(0, indexOf - 1);
		String swiftIs = swift.substring(indexOf);

		System.err.println("---->" + id1 + "---->" + swiftIs);
		String status = "";
		String switchT = "";
		if (swiftIs.equals("1")) {
			status = "已开启";
			switchT = "<div class='checkbox checkbox-slider--b'><label><input type='checkbox' name='payMethod' onclick='getradioo("
					+ id1 + "00)' checked><span></span></label></div>";
		} else if (swiftIs.equals("0")) {
			status = "已关闭";
			switchT = "<div class='checkbox checkbox-slider--b'><label><input type='checkbox' name='payMethod' onclick='getradioo("
					+ id1 + "01)'><span></span></label></div>";
		}

		int id = Integer.parseInt(id1);
		Equipment equipment = new Equipment();
		equipment.setId(id);
		equipment.setSwift(switchT);
		equipment.setStatus(status);
		equipmentService.update(equipment);

		PrintWriter out = response.getWriter();
		out.print(3);
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getSize() {
		return size;
	}

	public void setSize(int size) {
		this.size = size;
	}

	public String getSearch() {
		return search;
	}

	public void setSearch(String search) {
		this.search = search;
	}

	public String getSect1() {
		return sect1;
	}

	public void setSect1(String sect1) {
		this.sect1 = sect1;
	}

	public int getLine() {
		return line;
	}

	public void setLine(int line) {
		this.line = line;
	}

	public int getStartLine() {
		return startLine;
	}

	public void setStartLine(int startLine) {
		this.startLine = startLine;
	}

	public int getEndLine() {
		return endLine;
	}

	public void setEndLine(int endLine) {
		this.endLine = endLine;
	}

	public List<Equipment> getEquipmentList() {
		return equipmentList;
	}

	public void setEquipmentList(List<Equipment> equipmentList) {
		this.equipmentList = equipmentList;
	}

	public List<Equipment> getSearchList() {
		return searchList;
	}

	public void setSearchList(List<Equipment> searchList) {
		this.searchList = searchList;
	}

	@RequestMapping("/insert.action")
	public void insert() {
		Equipment equipment = new Equipment();
		for (int i = 26; i < 500; i++) {
			equipment.setId(i);
			equipment.setEqipName("Equip-Name." + i);
			equipment.setEqipId("Equip-No." + i);
			equipment.setSerial("Serial." + i);
			equipment.setElecRate("2." + i + "A");
			equipment.setElecVol("36." + i + "V");
			equipment.setStatus("Start");
			equipment.setSwift("Open");
			equipment.setInstall("D:/Install/place/" + i);
			equipmentService.insert(equipment);
		}
	}

	@RequestMapping("/startAll.action")
	public void startAll(HttpServletResponse response) throws ServletException,IOException {

		Equipment equipment = new Equipment();
		for (int i = 1; i < 500; i++) {
			equipment.setId(i);

			equipment.setStatus("已开启");
			equipment.setSwift(
					"<div class='checkbox checkbox-slider--b'><label><input type='checkbox' name='payMethod' onclick='getradioo("
							+ i + "00)' checked><span></span></label></div>");

			equipmentService.update(equipment);
		}
		
		PrintWriter out=response.getWriter();
		out.print(2);
	}

	@RequestMapping("/closeAll.action")
	public void closeAll(HttpServletResponse response) throws ServletException,IOException{

		Equipment equipment = new Equipment();
		for (int i = 1; i < 500; i++) {
			equipment.setId(i);

			equipment.setStatus("已关闭");
			equipment.setSwift(
					"<div class='checkbox checkbox-slider--b'><label><input type='checkbox' name='payMethod' onclick='getradioo("
							+ i + "01)'><span></span></label></div>");

			equipmentService.update(equipment);
		}
		
		PrintWriter out=response.getWriter();
		out.print(2);
	}

	@RequestMapping("/update.action")
	public void update() {
		Equipment equipment = new Equipment();
		for (int i = 1; i < 500; i++) {
			equipment.setId(i);
			equipment.setEqipName("Equip-Name." + i);
			equipment.setEqipId("Equip-No." + i);
			equipment.setSerial("Serial." + i);
			equipment.setElecRate("2." + i + "A");
			equipment.setElecVol("36." + i + "V");
			equipment.setStatus("Start");
			equipment.setSwift(
					"<div class='checkbox checkbox-slider--b'><label><input type='checkbox' name='payMethod' onclick='getradioo("
							+ i + "00)' checked><span></span></label></div>");
			equipment.setInstall("D:/Install/place/" + i);
			equipmentService.update(equipment);
		}
	}
}
